<template>
	<div>
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="900px">
			<el-form ref="faqDialogRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
				<el-form-item label="标题" prop="title">
					<el-input v-model="state.ruleForm.title" placeholder="请输入标题" clearable></el-input>
				</el-form-item>
				<el-form-item label="内容" prop="content">
					<el-input v-model="state.ruleForm.content" placeholder="请输入内容" :rows="4" type="textarea" clearable></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="gender">
					<el-radio-group v-model="state.ruleForm.faqStatus">
						<el-radio :label="0">正常</el-radio>
						<el-radio :label="1">隐藏</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(faqDialogRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { type FormInstance, type FormRules } from 'element-plus';
import { dataFaqApi } from '/@/api/shop/data/faq/index';
const emit = defineEmits(['refresh']);
import messageUtils from '/@/utils/messageUtils';
const faqDialogRef = ref();
const state = reactive({
	ruleForm: {
		id: '',
		title: '',
		content: '',
		faqStatus: 0,
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

const rules = reactive<FormRules>({
	activityName: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
	activityTime: [{ required: true, message: '请选择活动时间 ', trigger: 'blur' }],
});
const onCancel = () => {
	closeDialog();
};

const closeDialog = () => {
	console.log('关闭窗口');
	state.dialog.isShowDialog = false;
	faqDialogRef.value.resetFields();
};

const openDialog = async (type: string, id: string) => {
	state.dialog.isShowDialog = true;
	if (type === 'edit') {
		state.dialog.title = '修改问题';
		state.dialog.type = 'edit';
		state.dialog.submitTxt = '更新';
		const { data } = await dataFaqApi().view(id);
		state.ruleForm = data;
	} else {
		state.dialog.title = '创建问题';
		state.dialog.type = 'add';
		state.dialog.submitTxt = '新增';
	}
	state.dialog.isShowDialog = true;
};

const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate(async (valid) => {
		if (valid) {
			try {
				if ('edit' === state.dialog.type) {
					await dataFaqApi().update(state.ruleForm);
				} else {
					await dataFaqApi().create(state.ruleForm);
				}
				messageUtils().success('超过成功');
				closeDialog();
				emit('refresh');
			} catch (e) {
				messageUtils().error('操作失败,请重试');
			}
		}
	});
};
defineExpose({ openDialog });
</script>

<style scoped lang="scss"></style>
